<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\UsersReg;
use App\Models\UsersAttrib;

class LoginController extends Controller
{

    public function login()
    {
        //用户登录(邮箱，电话号码)       
        $username = request::input('username');
        $password = request::input('password'); 

        $resdata = UsersReg::where('email', $username)->orWhere('mobile', $username)->first();        

        if($resdata == null)
        {
            // 用户名不正确
            return 1;
        }

        // 加密后的密码
        $password = $this->compile_password($password, $resdata->salt); 
        if($password != $resdata->password)
        {
            // 密码不正确
            return 2;
        }

        // 会员介绍信息
        $resdata['attrib'] = UsersAttrib::where('uid', $resdata->uid)->first(); 

        // 登录成功返回
        return $resdata;
    }


    /**
     * 根据 salt 混淆密码
     *
     * @param  string
     * @param  string
     * @return string
     */
    public function compile_password($password, $salt)
    {
        $password = md5(md5($password) . $salt);
        return $password;
    }
    
}